<?php

class AdminController extends Controller
{
    public function actionIndex()
    {
        $this->redirect('admin/login');
    }

    public function actionLogin()
    {
        if (Yii::app()->user->isGuest) {
            $model = new User;
            // collect user input data
            if (isset($_POST['User'])) {
                $model->attributes = $_POST['User'];

                if ($model->validate()) {
                    $this->_lastVisit();
                    $this->redirect(Yii::app()->user->returnUrl);
                    //$this->redirect(array('/dashboard/index'));
                }
            }

            $this->render('/admin/login', array('model'=> $model));
        } else {
            $this->redirect(Yii::app()->user->returnUrl);
        }
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(array('/user/login'));
    }

    // Uncomment the following methods and override them if needed
    /*
     public function filters()
     {
         // return the filter configuration for this controller, e.g.:
         return array(
             'inlineFilterName',
             array(
                 'class'=>'path.to.FilterClass',
                 'propertyName'=>'propertyValue',
             ),
         );
     }

     public function actions()
     {
         // return external action classes, e.g.:
         return array(
             'action1'=>'path.to.ActionClass',
             'action2'=>array(
                 'class'=>'path.to.AnotherActionClass',
                 'propertyName'=>'propertyValue',
             ),
         );
     }
     */

    /**
     * Set last login user time
     */
    private function _lastVisit()
    {
        $lastVisit = User::model()->findByPk(Yii::app()->user->id);
        $lastVisit->lastvisit = time();
        $lastVisit->save();
    }
}